package wahadlo.nowe.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComponent;

import wahadlo.nowe.controllers.AxisController;
import wahadlo.nowe.controllers.Controller;
import wahadlo.nowe.model.Model;
import wahadlo.nowe.model.ModelRecipient;
import wahadlo.nowe.model.ModelSupplier;
import wahadlo.nowe.model.StandardModel;

public class ControllPanel extends JComponent {
	private static final long serialVersionUID = 1L;
	private JButton start;
	private JButton stop;
	private JButton resume;
	private List<ModelSupplier> modelSuppliers;
	private List<ModelRecipient> modelRecipients;

	private boolean isResume;
	private AxisActionController axisActionController;

	public ControllPanel(List<ModelSupplier> modelSuppliers,
			List<ModelRecipient> modelRecipients) {
		this.modelSuppliers = modelSuppliers;
		this.modelRecipients = modelRecipients;
		init();
	}

	private void init() {
		start = new JButton("Start");
		axisActionController = new AxisActionController();
		start.addActionListener(axisActionController);
		stop = new JButton("Stop");
		stop.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				start.setEnabled(true);
				AxisController axisController = (AxisController) axisActionController.axisController;
				axisController.getAnimation().setRunnig(false);
				for (ModelRecipient modelRecipient : modelRecipients) {
					if (modelRecipient instanceof AxisComponent) {
						AxisComponent axisComponent = (AxisComponent) modelRecipient;
						axisComponent.clean();
					}
				}
			}
		});

		resume = new JButton("Resume");

		resume.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				start.setEnabled(true);
				AxisController axisController = (AxisController) axisActionController.axisController;
				axisController.getAnimation().setResume(true);
				isResume = true;
			}
		});

		setLayout(new GridLayout(3, 1, 0, 2));
		add(start, 0);
		add(stop, 1);
		add(resume, 2);
	}

	private final class AxisActionController implements ActionListener {

		private Controller axisController;

		public void actionPerformed(ActionEvent e) {
			if (isResume) {
				AxisController axisController = (AxisController) axisActionController.axisController;
				axisController.getAnimation().setResume(false);
			} else {
				// blokujemy przycisk
				start.setEnabled(false);
				// przygotowujemy dane
				Model model = prepareModel();
				axisController = new AxisController();
				// wiążemy odbiorców z kontrolerem. To można zrobić przez
				// mapowanie
				// a la Spring
				for (ModelRecipient modelRecipient : modelRecipients) {
					if (modelRecipient instanceof AxisComponent) {
						((AxisComponent) modelRecipient).clean();
						axisController.putRecipient(modelRecipient);
						axisController.putModel(model);
					}
				}
				// wywołujemy wiązanie danych z obiektami w kontrolerze
				if (axisController.bind()) {
					// jak się powiedzie zaczynamy procesować dane
					axisController.process();
				} else {
					// jak nie zwracamy błąd i odblokowujemy przycisk
					System.out.println("Błąd!");
					start.setEnabled(true);
				}
			}
		}

		private Model prepareModel() {
			StandardModel model = new StandardModel();
			for (ModelSupplier ms : modelSuppliers) {
				model.add(ms.getModel());
			}
			return model;
		}
	}
}
